package cn.wowjoy.pda.android_pharmacy.mvvm.presenter;

import android.util.Log;

import java.util.List;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPillNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPillSingleBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalPillMedicineDetailViewModel;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/31.
 */

public class InHospitalPillMedicineDetailPresenter extends BasePresenter<InHospitalPillMedicineDetailViewModel> {
    private ApiService mApiService;
    private MDialog waitDialog;
    public  String DJJLID;
    public  String DJBQMC;
    public String Name;
    public String time;
    @Override
    public void onCreatePresenter() {
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
        DJJLID = (String) activity.getIntent().getExtras().getSerializable("DJJLID");
        DJBQMC = (String) activity.getIntent().getExtras().getSerializable("DJBQMC");
        loadData(DJJLID);
    }

    public void loadData(final String DJJLID){
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(InHospitalPillNode.getSingleContent("PDAQ11",DJJLID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
//                         waitDialog.dismiss();
                        //TODO:信息获取失败
                        if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
                            new MyToast(activity).showinfo("信息获取失败");
                            return;
                        }
                        //朱一新  00026
                        //TODO:门诊信息获取正确 跳转并传值
                        List<InHospitalPillSingleBeen> inHospitalPillSingleBeens = XmlUtils.getInHospitalPillSingleBeenList(xmlData.body.roleInfoResponse.retXml);
                        if(null != inHospitalPillSingleBeens && inHospitalPillSingleBeens.size()!=0){

                            RxBus.getInstance().post(Contants.INHOSPITAL_PILL_MEDICINE_DETAIL_RV_COUNT,inHospitalPillSingleBeens);
                            PreferenceManager.getInstance().setHospitalPillProgressTotal( inHospitalPillSingleBeens.size()+"");
                            viewModel.setWData(inHospitalPillSingleBeens);
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }

}
